package CelestialWeb;

import CelestialDataAccess.Product;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import CelestialDataAccess.*;
import CelestialDataStructures.*;

import java.util.UUID;
import java.util.Collection;

public class TrackOrderServlet extends HttpServlet {

    private WriteXMLError m_WriteError = new WriteXMLError();
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        String strOrderID = request.getParameter("order_id");

        response.setContentType("text/xml");
        response.setHeader("Cache-control", "no-cache");
        PrintWriter pw = response.getWriter();

        if(strOrderID==null){
            m_WriteError.writeSingleError(1, "No Order ID is supplied", pw);
            return;
        }
		  
		  CustomersDataAccess cusAccess = new CustomersDataAccess();
		  Customer curCust;
        ProductsDataAccess daProducts = new ProductsDataAccess();
        Product prod;
		  OrdersDataAccess orderAccess = new OrdersDataAccess();
		  Order curOrder;
		  int temp;	//helps double conversion
		  
		  try{
		  		curOrder = orderAccess.getOrder( UUID.fromString(strOrderID) );
		  }
		  catch(DataAccessException ex){
		  		m_WriteError.writeSingleError(16384, ex.getMessage(), pw);
				return;
		  }
		  
		  //if no orders found write an empty xml and return
		  if(curOrder==null){
		  		pw.write("<Details>");

                                pw.write("<CustomerID>_</CustomerID>");
		  		pw.write("<FirstName>_</FirstName>");
		  		pw.write("<LastName>_</LastName>");

                                pw.write("<tblOrders>");
                                pw.write("<OrderID>_</OrderID>");
                                pw.write("<OrderDate>_</OrderDate>");
                                pw.write("<DeliveryDate>_</DeliveryDate>");
                                pw.write("<Status>1</Status>");
                                pw.write("<NetAmount>_</NetAmount>");
                                pw.write("</tblOrders>");

                                pw.write("<tblOrderItems>");
                                pw.write("<OrderID>_</OrderID>");
                                pw.write("<ProductID>_</ProductID>");
                                pw.write("<Quantity>_</Quantity>");
                                pw.write("<UnitPrice>_</UnitPrice>");
                                pw.write("<PriceOverride>_</PriceOverride>");
                                pw.write("</tblOrderItems>");

                                pw.write("<tblProductInventory>");
                                pw.write("<Product>");
                                pw.write("<ProductID>_</ProductID>");
                                pw.write("<ItemName>_</ItemName>");
                                pw.write("</Product>");
                                pw.write("</tblProductInventory>");

		  		pw.write("</Details>");
		  }

//start of xml write		  
		  else{
		  		try{
		  	  		curCust = cusAccess.getCustomerByID( curOrder.getCustomerID() );
				}
				catch(DataAccessException ex){
			  		m_WriteError.writeSingleError(16384, ex.getMessage(), pw);
					return;
			   }
			  pw.write("<Details>");		  
			  pw.write("<CustomerID>"+ curCust.getCustomerID() +"</CustomerID>");
			  pw.write("<FirstName>"+ curCust.getFirstName() +"</FirstName>");
			  pw.write("<LastName>"+ curCust.getLastName() +"</LastName>");


			  pw.write("<tblOrders>");
			  pw.write("<OrderID>");
			  pw.write(curOrder.getOrderID().toString());			  
			  pw.write("</OrderID>");
			  pw.write("<OrderDate>");
			  pw.write(curOrder.getOrderDate().toString());
			  pw.write("</OrderDate>");
			  pw.write("<DeliveryDate>");
			  pw.write(curOrder.getDispatchDate().toString());
			  pw.write("</DeliveryDate>");
			  pw.write("<Status>");
			  pw.write("" + curOrder.getStatus());
			  pw.write("</Status>");
			  pw.write("<NetAmount>");
			  temp = (int)(curOrder.getTotalAmount()* 100);
			  pw.write("" + (temp/100.0) );
			  pw.write("</NetAmount>");
			  pw.write("</tblOrders>");			  

//access list of order items
			  Collection<OrderItem> orderItemList = curOrder.getOrderItems();
			  for(OrderItem ordItem : orderItemList){
					  pw.write("<tblOrderItems>");
					  pw.write("<OrderID>");
					  pw.write(ordItem.m_uidOrderItemID.toString());
					  pw.write("</OrderID>");
					  pw.write("<ProductID>");
					  pw.write(ordItem.m_strProductID);
					  pw.write("</ProductID>");
					  pw.write("<Quantity>");
					  pw.write("" + ordItem.m_iQuantity);
					  pw.write("</Quantity>");
					  pw.write("<UnitPrice>");
					  temp = (int)(ordItem.m_dblUnitPrice * 100);
					  pw.write("" + temp/100.0);
					  pw.write("</UnitPrice>");
					  pw.write("<PriceOverride>");
					  pw.write("" + ordItem.m_bPriceOverride);
					  pw.write("</PriceOverride>");
					  pw.write("</tblOrderItems>");
				}

//write for product
				pw.write("<tblProductInventory>");

				for(OrderItem ordItem : orderItemList){
					try{
						prod = daProducts.getProductByID(ordItem.m_uidOrderItemID.toString());
					}catch(DataAccessException ex){
						m_WriteError.writeSingleError(16384, ex.getMessage(), pw);
						return;
					}
					pw.write("<Product>");
					pw.write("<ProductID>");
					pw.write(prod.getProductID());
					pw.write("</ProductID>");
					pw.write("<ItemName>");
					pw.write(prod.getProductName());
					pw.write("</ItemName>");
					pw.write("</Product>");
				}

				pw.write("</tblProductInventory>");

		  	   pw.write("</Details>");
			}
			//end of all writing of xml
			pw.close();
		}
		  
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
